package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TNews;
import interfaces.INewsService;
import net.sf.json.JSONArray;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class NewsAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private INewsService newsService;

	private TNews newsBean;
	private List<TNews> newsList;

	private String newNo;
	private String newNos;
	private String newTitle;
	private String newAuthor;
	private String newInfo;
	private String newState;
	private Date newDate;
	private Date newDateTop;
	private Date newDateEnd;
	private String newRemark;

	// 展示对象
	public String show() throws Exception {
		if (!newsService.exist(newNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			newsBean = newsService.fetch(newNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!newsService.exist(newNo)) {
			newsBean = new TNews();
		} else {
			newsBean = newsService.fetch(newNo);
		}
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		if (!newsService.exist(newNo)) {
			newsBean = new TNews();
			newsBean.setNewNo(StringUtil.guid());
		} else {
			newsBean = newsService.fetch(newNo);
		}
		newsBean.setNewTitle(newTitle);
		newsBean.setNewAuthor("管理员");
		newsBean.setNewInfo(newInfo);
		newsBean.setNewState("1");
		newsBean.setNewDate(new Date());
		newsBean.setNewRemark("");
		newsService.persist(newsBean);
		return SUCCESS;
	}

	public String move() throws Exception {
		newsService.remove(newNo);
		return SUCCESS;
	}

	// AUTOCOMPLATE功能
	public void auto() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("newNo", StringUtil.split(newNo));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField("newNo");
		sortInfo.setSortOrder(new Boolean(true));

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TNews> resultSet = newsService.search(querySet);
		newsList = resultSet.getResultList();

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().println(JSONArray.fromObject(newsList).toString());
	}

	// 列表展示
	// paramMap为查询条件,sortInfo为排序方式,pageInfo为分页对象
	// paramMap可为空,sortInfo可为空,pageInfo不能为空,如果需要查询全部则设置pageIndex为1,pageSize为Integer.MAX_VALUE
	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("newNo", StringUtil.split(newNo));
		paramMap.put("newTitle", newTitle);
		paramMap.put("newAuthor", newAuthor);
		paramMap.put("newInfo", newInfo);
		paramMap.put("newState", newState);
		paramMap.put("newDate", ParameterUtil.dateCombin(newDateTop, newDateEnd));
		paramMap.put("newRemark", newRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "newDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TNews> resultSet = newsService.search(querySet);
		newsList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("newNo", StringUtil.split(newNo));
		paramMap.put("newTitle", newTitle);
		paramMap.put("newAuthor", newAuthor);
		paramMap.put("newInfo", newInfo);
		paramMap.put("newState", newState);
		paramMap.put("newDate", ParameterUtil.dateCombin(newDateTop, newDateEnd));
		paramMap.put("newRemark", newRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "newDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TNews> resultSet = newsService.search(querySet);
		newsList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("标题");
		fieldHead.add("作者");
		fieldHead.add("内容");
		fieldHead.add("状态");
		fieldHead.add("创建日期日期");
		fieldHead.add("创建日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TNews newsBean : newsList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(newsBean.getNewNo());
			fieldItem.add(newsBean.getNewTitle());
			fieldItem.add(newsBean.getNewAuthor());
			fieldItem.add(newsBean.getNewInfo());
			fieldItem.add((newsBean.getNewState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(newsBean.getNewDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(newsBean.getNewDate(), DateUtil.TIME));
			fieldItem.add(newsBean.getNewRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public INewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(INewsService newsService) {
		this.newsService = newsService;
	}

	public TNews getNewsBean() {
		return newsBean;
	}

	public void setNewsBean(TNews newsBean) {
		this.newsBean = newsBean;
	}

	public List<TNews> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<TNews> newsList) {
		this.newsList = newsList;
	}

	public String getNewNo() {
		return newNo;
	}

	public void setNewNo(String newNo) {
		this.newNo = newNo;
	}

	public String getNewNos() {
		return newNos;
	}

	public void setNewNos(String newNos) {
		this.newNos = newNos;
	}

	public String getNewTitle() {
		return newTitle;
	}

	public void setNewTitle(String newTitle) {
		this.newTitle = newTitle;
	}

	public String getNewAuthor() {
		return newAuthor;
	}

	public void setNewAuthor(String newAuthor) {
		this.newAuthor = newAuthor;
	}

	public String getNewInfo() {
		return newInfo;
	}

	public void setNewInfo(String newInfo) {
		this.newInfo = newInfo;
	}

	public String getNewState() {
		return newState;
	}

	public void setNewState(String newState) {
		this.newState = newState;
	}

	public Date getNewDate() {
		return newDate;
	}

	public void setNewDate(Date newDate) {
		this.newDate = newDate;
	}

	public Date getNewDateTop() {
		return newDateTop;
	}

	public void setNewDateTop(Date newDateTop) {
		this.newDateTop = newDateTop;
	}

	public Date getNewDateEnd() {
		return newDateEnd;
	}

	public void setNewDateEnd(Date newDateEnd) {
		this.newDateEnd = newDateEnd;
	}

	public String getNewRemark() {
		return newRemark;
	}

	public void setNewRemark(String newRemark) {
		this.newRemark = newRemark;
	}

}
